package com.剑指Offer;

import java.util.Stack;

/**
 * 用两个栈实现队列
 */
public class 两个栈实现队列 {
	public static void main(String[] args) {
		Cquque<Integer> cquque=new Cquque<>();
		cquque.appendTail(1);
		cquque.appendTail(5);
		cquque.appendTail(6);
		cquque.appendTail(8);
		cquque.appendTail(10);
		System.out.println(cquque.deleteHead());
		System.out.println(cquque.deleteHead());
		cquque.appendTail(10);
		cquque.appendTail(10);
		System.out.println(cquque.deleteHead());
		System.out.println(cquque.deleteHead());
	}
}

class Cquque<T> {
	private Stack<T> s1;
	private Stack<T> s2;

	public void appendTail(T node) {
		if (s1 == null)
			s1 = new Stack<>();
		s1.push(node);
	}

	public T deleteHead() {
		if (s2 == null) {
			s2 = new Stack<>();
		}
		if (s1.size() == 0 && s2.size() == 0) {
			System.out.println("The queue is empty!");
			return null;
		}
		if (s2.size() != 0) {
			return s2.pop();
		}
		while (s1.size() != 0) {
			s2.push(s1.pop());
		}
		return s2.pop();
	}

}
